package com.markerhub.controller;


import com.markerhub.common.lang.Result;
import com.markerhub.common.lang.retunData;
import com.markerhub.entity.TPayDefary;
import com.markerhub.entity.TPayOrder;
import com.markerhub.povo.TPayOrderVo;
import com.markerhub.service.TPayDefaryService;
import com.markerhub.service.TPayOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 刘佳明
 * @since 2022-01-11
 */
@RestController
@RequestMapping("/tPayOrder")
@CrossOrigin
public class TPayOrderController {

    @Autowired
    TPayOrderService tPayOrderService;



    @Autowired
    TPayDefaryService tPayDefaryService;

    @GetMapping("/list")
    @PreAuthorize("hasAuthority('sys:order:list')")
    //@ResponseBody
    public retunData list(Integer page, Integer limit, String number , String orderNumber, String projectid, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date1,
                          @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date2, String defarystatus, Integer Userids) throws ParseException {

        //https://blog.csdn.net/jingsong2015/article/details/77714780
        // System.out.println(page);
        System.out.println(limit);
        System.out.println(projectid);
        System.out.println(orderNumber);
        System.out.println(defarystatus);

        LocalDate dateup = date1;
        LocalDate  datedown = date2;
        System.out.println(date1);
        System.out.println(date2);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd ");

//        if (date1!=null){
//            //dateup = simpleDateFormat.parse(date1);
//            DateTime beginOfDay = DateUtil.beginOfDay(DateUtil.parse(date1));
//            dateup = beginOfDay.toTimestamp().toLocalDateTime();
//
//        }
//        if (date2!=null){
//           // datedown = simpleDateFormat.parse(date2);
//            DateTime beginOfDay = DateUtil.beginOfDay(DateUtil.parse(date2));
//            datedown = beginOfDay.toTimestamp().toLocalDateTime();
//        }
        if (page==0&&limit==0){
            List<TPayOrderVo> list = tPayOrderService.getOrderList(0,0,number,orderNumber,projectid,dateup,datedown,defarystatus,Userids);
            retunData retunData1 = new retunData();
            retunData1.setData(list);
            return retunData1;
        }
        System.out.println(dateup+"LocalDateTime");
        System.out.println(datedown +"LocalDateTime");
        Integer pages=(page-1)*limit;

        List<TPayOrderVo> TFacilityBaseVoList= tPayOrderService.getOrderList(pages,limit,number,orderNumber,projectid,dateup,datedown,defarystatus,Userids);
        List<TPayOrderVo> count= tPayOrderService.getOrderList(0,0,number,orderNumber,projectid,dateup,datedown,defarystatus,Userids);
        // System.out.println("List<TFacilityBaseVo> count "+count);
        retunData retunData = new retunData();
        retunData.setData(TFacilityBaseVoList);
        retunData.setCount(count.size());


        return retunData;
    }


    @RequestMapping("/PlaceOrder")
    @PreAuthorize("hasAuthority('sys:order:PlaceOrder')")
    @Transactional
    public Result PlaceOrder(Integer userInfoid, String baseid, String functionid ) {

        System.out.println(userInfoid);
        System.out.println(baseid);
        System.out.println(functionid);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String date = simpleDateFormat.format(new Date());//下单时间，精确到秒
        System.out.println("生成时间戳 "+ date);
        TPayOrder tPayOrder = new TPayOrder();
        tPayOrder.setBaseid(baseid);
        tPayOrder.setOrderNumber(date);
        tPayOrder.setFunctionid(functionid);
        tPayOrder.setOrderStatus("1");
        tPayOrder.setUserobject(String.valueOf(userInfoid));
        tPayOrder.setCreateTime(LocalDateTime.now());
        tPayOrder.setDeleteState(1);
        boolean save = tPayOrderService.save(tPayOrder);

        TPayDefary tPayDefary = new TPayDefary();
        tPayDefary.setDefaryUserobject(String.valueOf(userInfoid));
        tPayDefary.setDefarytime(LocalDateTime.now());
        tPayDefary.setDefarytype(1);
        tPayDefary.setDefarystatus(1);
        tPayDefary.setOrderid(tPayOrder.getOrderid());
        boolean save1 = tPayDefaryService.save(tPayDefary);
        return Result.succ(null);
    }

//    @ApiOperation(value = "订单信息导出", notes = "订单信息导出")
//    @PostMapping(value = "/export")
//    @PreAuthorize("hasAuthority('sys:order:export')")
//    public void exportXls(@RequestBody getOrderVo requireVo, HttpServletResponse response) throws IOException {
//        String number = requireVo.getNumber();
//        String projectid = requireVo.getProjectid();
//        String orderNumber = requireVo.getOrderNumber();
//        LocalDate dateup = requireVo.getDate1();
//        LocalDate datedown = requireVo.getDate2();
//        String defarystatus = requireVo.getDefarystatus();
//        // 这里的业务数据根据自己的业务场景去做查询
//        List<TPayOrderVo> list = tPayOrderService.getOrderList(0,0,number,orderNumber,projectid,dateup,datedown,defarystatus);
//        List<TPayOrderVos>  tPayOrderVos = new ArrayList<>();
//        for (TPayOrderVo TPayOrderVo:list ) {
//            TPayOrderVos tPayOrderVos1 = new TPayOrderVos();
//            BeanUtils.copyProperties(TPayOrderVo,tPayOrderVos1);
//            tPayOrderVos.add(tPayOrderVos1);
//        }
//
//
//        System.out.println(tPayOrderVos);
//        System.out.println("需要导出的数据");
//
//        response.setContentType("application/vnd.ms-excel");
//        response.setCharacterEncoding("utf-8");
//        String fileName = URLEncoder.encode("测试", "UTF-8");
//        response.setHeader("Content-disposition", "attachment;filename="+fileName+".xlsx");
//        EasyExcel.write(response.getOutputStream()).sheet("第一个sheet").doWrite(tPayOrderVos);
//
//
//    }

    /* 逻辑删除订单   */
    @RequestMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('sys:order:delete')")
    @ResponseBody
    @Transactional
    public  Result delete(@PathVariable("id") String id){
        System.out.println("sys:reqair:delete === >>"+id);
        TPayOrder byId = tPayOrderService.getById(id);
        if (byId.getDeleteState()==1){
            byId.setDeleteState(0);
            boolean b = tPayOrderService.updateById(byId);
            //  boolean id1 = tFacilityFunctionService.remove(new QueryWrapper<TFacilityFunction>().eq("baseid", id));
            return Result.succ(null);
        }else {

            return Result.fail("当前订单信息已经逻辑删除！！");
        }

    }
}

